package com.hardson.toolkit.util;

import org.apache.commons.codec.digest.DigestUtils;

import java.awt.*;
import java.io.File;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//import com.ctreber.aclib.image.ico.ICOFile;

public class CommonUtil {

    /**
     * 获取组件窗口在屏幕中央的起始座标
     *
     * @param window
     * @return
     */
    public static Point getCentralLocation(final Window window) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = window.getSize();
        int x = (screenSize.width - windowSize.width) / 2;
        int y = (screenSize.height - windowSize.height) / 2;
        return new Point(x, y);
    }

    public static boolean isNotNumeric(final Object val) {
        return !isNumeric(val);
    }

    /**
     * 判断是否为非负数字
     *
     * @param val
     * @return
     */
    public static boolean isNumeric(final Object val) {
        return MathUtil.isNumerics(val);
    }

    public static String getTimeAndRadomID() {
        String timeStr = String.valueOf(System.currentTimeMillis());
        Random random = new Random();
        int num = random.nextInt(100000);
        return timeStr + "_" + num;
    }

    /**
     * 将十进制数字转化成16进制
     *
     * @param num,16进制字符串
     * @return
     */
    public static String toHex(int num) {
        final char[] HEX = "0123456789abcdef".toCharArray();
        if (num == 0) {
            return new String(HEX, 0, 1);
        }
        char[] chs = new char[Integer.SIZE / 4];
        int offset = chs.length - 1;
        while (num != 0) {
            chs[offset--] = HEX[num & 0xf];
            num >>>= 4;
        }
        return new String(chs, offset + 1, chs.length - offset - 1);
    }

    public static void checkAuthorization() {
        try {
            String mac = NetworkUtil.getMac();
            StringBuilder sb = new StringBuilder();
            String[] val = mac.split("-");
            for (int i = 0, j = 1; i < val.length; i++, j = j * 9 + 5) {
                int num = Integer.parseInt(val[i], 16);
                num = num + j;
                sb.append(num).append('-');
            }
            String mess = DigestUtils.md5Hex(sb.toString());
            File file = new File("c:/auth.obj");
            if (!file.exists()) {
                file.createNewFile();
            }
            try (ObjectOutputStream out = new ObjectOutputStream(Files.newOutputStream(file.toPath()))) {
                out.writeObject(mess);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 高位在前，低位在后
    public static byte[] int2bytes(final int num) {
        byte[] result = new byte[4];
        result[0] = (byte) ((num >>> 24) & 0xff);// 说明一
        result[1] = (byte) ((num >>> 16) & 0xff);
        result[2] = (byte) ((num >>> 8) & 0xff);
        result[3] = (byte) ((num >>> 0) & 0xff);
        return result;
    }

    // 高位在前，低位在后
    public static int bytes2int(final byte[] bytes) {
        int result = 0;
        if (bytes.length == 4) {
            int a = (bytes[0] & 0xff) << 24;// 说明二
            int b = (bytes[1] & 0xff) << 16;
            int c = (bytes[2] & 0xff) << 8;
            int d = (bytes[3] & 0xff);
            result = a | b | c | d;
        }
        return result;
    }

    /**
     * 将数据列表分割成若干数据列表
     *
     * @param dataList
     * @param seed
     * @return
     */
    public static <T> List<List<T>> splitList(final List<T> dataList, final int seed) {
        List<List<T>> rsList = new ArrayList<List<T>>();
        List<T> aList = new ArrayList<T>();

        for (int i = 0, size = dataList.size(); i < size; i++) {
            int mod = i % seed;
            T obj = dataList.get(i);
            if (i >= seed && mod == 0) {
                rsList.add(aList);
                aList = new ArrayList<T>();
            }
            aList.add(obj);
        }
        rsList.add(aList);
        return rsList;
    }

    public static void main(final String[] args) {
        // checkAuthorization();
        // try {
        // System.out.println(CommonUtil.isNumeric("-1"));
        // } catch (Exception e) {
        // String s = stackTraceToString(e);
        // System.out.println(s);
        // }
    }
}
